package org.jeecg.modules.interview.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.activity.entity.ActivityInfo;
import org.jeecg.modules.activity.service.IActivityInfoService;
import org.jeecg.modules.content.entity.ContentInfo;
import org.jeecg.modules.content.service.IContentInfoService;
import org.jeecg.modules.engine.service.IWorkflowEngineService;
import org.jeecg.modules.interview.entity.InterviewInfo;
import org.jeecg.modules.interview.entity.InterviewUnion;
import org.jeecg.modules.interview.service.IInterviewInfoService;
import org.jeecg.modules.interview.service.IInterviewUnionService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.task.entity.TaskInfoBase;
import org.jeecg.modules.task.service.ITaskInfoBaseService;
import org.jeecg.modules.workflow.entity.WorkFlowBase;
import org.jeecg.modules.workflow.entity.WorkFlowBaseAssign;
import org.jeecg.modules.workflow.service.IWorkFlowBaseAssignService;
import org.jeecg.modules.workflow.service.IWorkFlowBaseService;
import org.jeecg.util.SeqGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 采访
 * @author: wufanxin
 * @date: 2021-06-21
 * @version: V1.0
 */
@Slf4j
@RestController
@RequestMapping("/task/interviewInfo")
@Api(tags = "采访")
public class InterviewController extends JeecgController<InterviewInfo, IInterviewInfoService> {
	@Autowired
	private IInterviewInfoService interviewInfoService;

	@Autowired
	private IInterviewUnionService interviewUnionService;

	@Autowired
	private ITaskInfoBaseService taskInfoBaseService;

	@Autowired
	private IContentInfoService contentInfoService;

	@Autowired
	private IActivityInfoService activityInfoService;

	/**
	 * 查询任务相关的采访列表

	 * @param req
	 * @return
	 */
	@ApiOperation(value = "采访节点-查询任务相关的采访列表", notes = "采访节点-查询任务相关的采访列表")
	@GetMapping(value = "/queryInterviewList")
	public Result<?> queryInterviewList(HttpServletRequest req,@RequestParam(name="taskNo") String taskNo) {
		QueryWrapper<InterviewInfo> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("task_no",taskNo).orderByDesc("create_time");
		return Result.ok(interviewInfoService.list(queryWrapper));
	}


	/**
	 * 新增访问信息
	 * @param req
	 * @return
	 */
	@ApiOperation(value = "采访节点-新增访问信息", notes = "采访节点-新增访问信息")
	@PostMapping(value = "/saveInterviewInfo")
	public Result<?> saveInterviewInfo(HttpServletRequest req,@RequestBody InterviewInfo interviewInfo) {
		interviewInfoService.save(interviewInfo);
		return Result.ok();
	}



	/**
	 * 保存采访初稿
	 * @param req
	 * @param interviewDraft taskNo,draft,interviewUnion(userId,userName,taskNo)
	 * @return
	 */
	@ApiOperation(value = "采访节点-保存采访初稿（废弃）", notes = "采访节点-保存采访初稿（废弃）")
	@PostMapping(value = "/saveInterviewDraft")
	public Result<?> saveInterviewDraft(HttpServletRequest req,@RequestBody Map<String,Object> interviewDraft) {
		//根据taskNo 获取task信息
		QueryWrapper<TaskInfoBase> taskInfoBaseQueryWrapper = new QueryWrapper<>();
		taskInfoBaseQueryWrapper.eq("task_no",interviewDraft.get("taskNo")).last(" limit 1");
		TaskInfoBase taskInfoBase = taskInfoBaseService.getOne(taskInfoBaseQueryWrapper);

		if(taskInfoBase.getTaskType().equals("0")){
			UpdateWrapper<ContentInfo> contentInfoUpdateWrapper = new UpdateWrapper<>();
			contentInfoUpdateWrapper.set("content",interviewDraft.get("draft"));
			contentInfoUpdateWrapper.eq("task_id",interviewDraft.get("taskNo"));
			contentInfoService.update(contentInfoUpdateWrapper);
		}else{
			UpdateWrapper<ActivityInfo> activityInfoUpdateWrapper = new UpdateWrapper<>();
			activityInfoUpdateWrapper.set("content",interviewDraft.get("draft"));
			activityInfoUpdateWrapper.eq("task_id",interviewDraft.get("taskNo"));
			activityInfoService.update(activityInfoUpdateWrapper);
		}
		//清除联合撰稿人
		QueryWrapper<InterviewUnion> unionQueryWrapper = new QueryWrapper<>();
		unionQueryWrapper.eq("task_no",interviewDraft.get("taskNo"));
		interviewUnionService.remove(unionQueryWrapper);
		//保存撰稿人
		List<InterviewUnion> unionList = (List<InterviewUnion>)interviewDraft.get("interviewUnion");
		interviewUnionService.saveBatch(unionList);
		return Result.ok();
	}




	/**
	 * 查询采访初稿
	 * @param req
	 * @param taskNo
	 * @return
	 */
	@ApiOperation(value = "采访节点-查询采访初稿", notes = "采访节点-查询采访初稿")
	@GetMapping(value = "/queryInterviewDraft")
	public Result<?> queryInterviewDraft(HttpServletRequest req,@RequestParam String taskNo) {
		//根据taskNo 获取task信息
		QueryWrapper<TaskInfoBase> taskInfoBaseQueryWrapper = new QueryWrapper<>();
		taskInfoBaseQueryWrapper.eq("task_no",taskNo).last(" limit 1");
		TaskInfoBase taskInfoBase = taskInfoBaseService.getOne(taskInfoBaseQueryWrapper);

		String content = "";
		//获取content
		if(taskInfoBase.getTaskType().equals("0")){
			QueryWrapper<ContentInfo> contentInfoUpdateWrapper = new QueryWrapper<>();
			contentInfoUpdateWrapper.eq("task_id",taskNo).last(" limit 1");
			ContentInfo contentInfo = contentInfoService.getOne(contentInfoUpdateWrapper);
			content = null != contentInfo ? contentInfo.getContent():"";
		}else{
			QueryWrapper<ActivityInfo> activityInfoQueryWrapper = new QueryWrapper<>();
			activityInfoQueryWrapper.eq("task_id",taskNo).last(" limit 1");
			ActivityInfo activityInfo = activityInfoService.getOne(activityInfoQueryWrapper);
			content = null != activityInfo? activityInfo.getContent():"";
		}

		QueryWrapper<InterviewUnion> unionQueryWrapper = new QueryWrapper<>();
		unionQueryWrapper.eq("task_no",taskNo);
		List<InterviewUnion> unionList =  interviewUnionService.list(unionQueryWrapper);

		Map<String,Object> resultMap = new HashMap<>();
		resultMap.put("taskNo",taskNo);
		resultMap.put("interviewUnion",unionList);
		resultMap.put("draft",content);
		return Result.ok(resultMap);
	}







}
